// pages/rank/rank.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        array: [],
        userInfo: {
            avatarUrl: "", //用户头像
            nickName: "", //用户昵称
        },
        imgwidth: 0,
        imgheight: 0,
        himgwidth: 0,
        himgheight: 0,
        backgroundUrl: ""

    },

    praise_tap: function (e) {
        console.log(e.currentTarget.dataset.name)
        var id = e.currentTarget.dataset.id;
        var flag = e.currentTarget.dataset.flag;
        var num = e.currentTarget.dataset.num;
        var cookie_id = wx.getStorageSync('praise') || [];

        var newmessage = [];

        var tflag = "array[" + id + "].flag";
        var tNum = "array[" + id + "].praiseNum";
        console.log(flag);

        if (cookie_id.includes(id)) {//说明已经点过赞，取消赞
            var n = 0;
            for (var j in cookie_id) {
                if (cookie_id[j] != id) {
                    newmessage[n] = cookie_id[j];
                    n++;
                }
            }
            wx.setStorageSync('praise', newmessage);//删除取消赞的id
        } else {
            cookie_id.unshift(id);
            wx.setStorageSync('praise', cookie_id);
        }


        if (flag == true) {
            num -= 1;
            this.setData({
                [tflag]: false,
                [tNum]: num
            })
        } else {
            num += 1;
            this.setData({
                [tflag]: true,
                [tNum]: num
            })
        }
        wx.request({
            url: 'http://localhost:8080/eb-back-web/updateLike',
            method: 'post',
            header: {
                'content-type': 'application/x-www-form-urlencoded'
            },
            data: {
                like: num,
                nickname: e.currentTarget.dataset.name
            },
            success: function (res) {
            },
            fail: function () {
            }
        })
    },
    onLoad: function (options) {
        var _this = this;
        wx.getUserInfo({
            success: function (res) {
                console.log(res);
                var avatarUrl = 'userInfo.avatarUrl';
                var nickName = 'userInfo.nickName';
                _this.setData({
                    [avatarUrl]: res.userInfo.avatarUrl,
                    [nickName]: res.userInfo.nickName,
                })
            }
        })
        /**
         * 请求设置array数组
         */
        console.log("here----------------------1")

        wx.request({
            url: 'http://localhost:8080/eb-back-web/ranking',
            success: function (res) {
                _this.setData({
                    array: res.data,
                    backgroundUrl: getApp().globalData.backgroundUrl
                })
            },
            fail: function (res) {
                _this.setData({})
            }

        })
        console.log("here"+res.data);
        //读取本地缓存设置点赞flag
        var cookie_id = wx.getStorageSync('praise') || [];
        if (cookie_id != []) {
            for (var j in cookie_id) {
                var tid = cookie_id[j];
                var tflag = "array[" + tid + "].flag";
                this.setData({
                    [tflag]: true
                })
            }
        } else {
            console.log("cookie is null");
        }

        console.log("here----------------------3")

    },
    imageLoad: function (e) {
        var _this = this;
        var $width = e.detail.width,    //获取图片真实宽度
            $height = e.detail.height,
            ratio = $width / $height;   //图片的真实宽高比例

        console.log(ratio);
        if ($width > $height) {
            var viewHeight = 60;
            var viewWidth = viewHeight * ratio;
        } else {
            var viewWidth = 60;
            var viewHeight = 60 / ratio;
        }

        _this.setData({
            imgwidth: viewWidth,
            imgheight: viewHeight
        })
    },
    headimageLoad: function (e) {
        var _this = this;
        var $width = e.detail.width,    //获取图片真实宽度
            $height = e.detail.height,
            ratio = $width / $height;   //图片的真实宽高比例
        if ($width > ($height*1.25)) {
            var viewHeight = 600;
            var viewWidth = 600 * ratio;
        } else {
            var viewWidth = 750;
            var viewHeight = 750 / ratio;
        }

        _this.setData({
            himgwidth: viewWidth,
            himgheight: viewHeight
        })
    },


    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }

})
